const App = {
  data() {
    return {
      isParentDisabled: false,
      party_organization: {
        // Party_Organization_SerialNumber: "", //党组织编号
        // Party_Organization_Name: "", //党组织名称
        // Establishment_Date: "", //成立日期
        // Parent_Serialnumber: "", //上级组织编号
        // Party_Organization_Type: "", //组织类型
        // Party_Organization_Level: "", //党组级别 只有党委选择
        // Party_Organization_Address: "", //党组地址
        // Party_Organization_Coords: "", //党组所在地坐标集
        // Party_Organization_Captain_Number: "", //党组负责人编号
        // Enterprise_SeriralNumber: "", //党组所在企业编号
      },
      rules: {
        Party_Organization_Name: [
          { required: true, message: "党组织名称不能为空", trigger: "blur" },
          { min: 2, max: 50, message: "长度在2-50个字符", trigger: "blur" },
        ],
        Establishment_Date: [
          { required: true, message: "请选择成立日期", trigger: "change" },
        ],
        Party_Organization_Type: [
          { required: true, message: "请选择组织类型", trigger: "change" },
        ],
        Party_Organization_Level: [
          { required: true, message: "请选择党组级别", trigger: "change" },
        ],
        Party_Organization_Address: [
          { required: true, message: "请输入党组地址", trigger: "blur" },
          { min: 5, max: 100, message: "长度在5-100个字符", trigger: "blur" },
        ],
        Party_Organization_Captain_Number: [
          { required: true, message: "请输入负责人编号", trigger: "blur" },
        ],
      },
      value: [], // 初始化 labelNumber 属性
      hjoptions: [], //省市区县
      Nation: [], //民族
      Type: [
        {
          value: "党委",
          label: "党委",
        },
        {
          value: "总支",
          label: "总支",
        },
        {
          value: "支部",
          label: "支部",
        },
      ],
      Manager: [], //上级党组
      Level: [
        //党组级别
        {
          value: "省",
          label: "省",
        },
        {
          value: "市",
          label: "市",
        },
        {
          value: "区/县",
          label: "区/县",
        },
        {
          value: "乡镇/街道",
          label: "乡镇/街道",
        },
        {
          value: "村",
          label: "村",
        },
      ],
      Enterprise: [], //党组所在企业
    };
  },
  mounted() {
    //通过url方式把那个页面获取的党组织编号传过来
    let ls_URL = window.location.href;
    let li_CharIndex = ls_URL.indexOf("?") + 1;
    let ls_URLParameters = ls_URL.substring(li_CharIndex);
    let urlParams = new URLSearchParams(ls_URLParameters);
    let ls_RequesterlD = urlParams.get("parentSerial"); //解析的值
    console.log(ls_RequesterlD);
    // 将 parentSerial 赋值给表单模型
    this.party_organization.Parent_Serialnumber = ls_RequesterlD;
    console.log(this.party_organization.Parent_Serialnumber);

    let TABLE_NAME = sessionStorage.getItem("TABLE_NAME");
    console.log(TABLE_NAME);
    this.Data = TABLE_NAME;
    let key = sessionStorage.getItem("key");
    let type = sessionStorage.getItem("type");
    //如果点击的是编辑，调用编辑方法，否则走添加方法
    if (type == "Edit") {
      this.Edit(key);
    }
    this.Add(key);
    this.ceshi()
  },
  methods: {
    //查询表的字段
    ceshi() {
      let _this = this;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalQueryForIntiObject`,
        method: "get",
        data: {
          TableName: "party_fees",
        },
        success: (res) => {
          _this.party_fees = res[0];
        },
      });
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        method: "post",
        data: {
          Data: 'government_agencies',
          Condition: `Row_Status='0'`,
        },
        success: (res) => {
          this.Enterprise = res;
        }
      });
    },
    handleTypeChange(selectedValue) {
      console.log(selectedValue);
      if (selectedValue === "党委") {
        //当选择"党委"时清空上级党组织
        this.party_organization.Parent_Serialnumber = "";
        // 禁用上级党组织选择器（需在模板添加 :disabled）
        this.isParentDisabled = true;
        this.$message.warning("党委不能有上级党组织");
      } else {
        this.isParentDisabled = false;
      }
    },
    //添加进来的方法  筛掉删除的数据
    Add(key) {
      console.log("add", this.party_organization.Parent_Serialnumber);
      const _this = this;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: "party_organization", //表名
          Condition: " Row_Status='0'",
        },
        success: (res) => {
          console.log("res", res);
          //把查到值赋给Manager数组，然后绑定到下拉框，html页那边进行遍历
          _this.Manager = res;
          console.log("_this", _this.Manager);
        },
        error: (err) => {
          console.log(err);
        },
      });
    },
    //编辑进来方法
    Edit(key) {
      console.log(this.party_organization.Parent_Serialnumber);
      const _this = this;
      $.ajax({
        headers: {
          "content-type": "application/x-www-form-urlencoded",
        },
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: "party_organization", //表名
          Condition:
            "Party_Organization_SerialNumber = '" +
            this.party_organization.Parent_Serialnumber +
            "'",
        },
        success: (res) => {
          console.log(res);
          _this.party_organization = res[0];
        },
        error: (err) => {
          console.log(err);
        },
      });
    },
    //提交表单
    handleSubmit() {
      if (this.party_fees.Row_Status == "") {
        this.party_fees.Row_Status = "0";
      }
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          console.log(this.party_organization);
          if (this.party_organization.Parent_Serialnumber == null) {
            this.party_organization.Parent_Serialnumber = "";
          }
          // return
          let type = sessionStorage.getItem("type");
          console.log(type);
          const appElement = document.getElementById("MainTable");
          console.log(appElement);
          let dataOptions = appElement.getAttribute("data-options");
          console.log(dataOptions);
          //动态获取到的表单数据
          let options = JSON.parse(dataOptions);
          console.log("options", options);
          let lArray = new Array();
          lArray.push(this[options.form]);
          //动态获取的表名
          let bm = options.TableName;
          console.log(bm);
          let lJSONObject = new Object();
          lJSONObject[bm] = lArray; //换表
          console.log(type);
          // return
          let lJSONObject_DataSet = new Object(); // Modify
          if (type == "Edit") {
            lJSONObject_DataSet.Modify = lJSONObject; //换表
          } else {
            lJSONObject_DataSet.Add = lJSONObject; //换表
          }
          this.AddApi(lJSONObject_DataSet);
        } else {
          ElMessage.error("请完善表单信息");
          return false;
        }
      });
    },
    AddApi(lJSONObject_DataSet) {
      console.log(lJSONObject_DataSet);
      // return
      const _this = this;
      $.ajax({
        url: `${mh}/clientOperation/generalSaveForClient`,
        method: "post",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: (res) => {
          console.log(res);
          // 在普通 JavaScript 中使用 MessageBox
          ElMessage({
            message: "提交成功",
            type: "success",
            plain: true,
            duration: 1000, // 消息显示时长（毫秒）
          });
          // 等待 3 秒后回退页面
          setTimeout(() => {
            window.history.back();
          }, 1000);
        },
        error: (err) => {
          console.log(err);
        },
      });
    },
    goBack() {
      window.history.back();
    },
  },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
